package com.dci.intellij.dbn.browser.ui;

import com.dci.intellij.dbn.common.environment.EnvironmentType;
import com.dci.intellij.dbn.common.ui.tab.TabbedPane;
import com.dci.intellij.dbn.common.util.CommonUtil;
import com.dci.intellij.dbn.connection.ConnectionBundle;
import com.dci.intellij.dbn.connection.ConnectionHandler;
import com.dci.intellij.dbn.connection.ConnectionId;
import com.dci.intellij.dbn.connection.ConnectionManager;
import com.dci.intellij.dbn2.extension.DBObjectBundleListener;
import com.dci.intellij.dbn2.extension.ExtensionPointUtils;
import com.dci.intellij.dbn2.utils.ReflectionUtils;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBList;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.ui.tabs.TabsListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;

public class SpyTabbedBrowserForm extends com.dci.intellij.dbn.browser.ui.TabbedBrowserForm {

    public SpyTabbedBrowserForm(BrowserToolWindowForm parentComponent, @Nullable TabbedBrowserForm previous) {
        super(parentComponent, previous);


        ReflectionUtils.setFieldValue(this, "connectionTabs", new TabbedPane(this));
        initTabs(previous);
        getConnectionTabs().addListener(new TabsListener() {
            public void selectionChanged(TabInfo oldSelection, TabInfo newSelection) {
            }

            public void beforeSelectionChanged(TabInfo oldSelection, TabInfo newSelection) {
            }

            public void tabRemoved(@NotNull TabInfo tabInfo) {
            }

            public void tabsMoved() {
            }
        });
    }

    private void initTabs(@Nullable TabbedBrowserForm previous) {
        Project project = this.getProject();
        ConnectionManager connectionManager = ConnectionManager.getInstance(project);
        ConnectionBundle connectionBundle = connectionManager.getConnectionBundle();
        Iterator connectIter = connectionBundle.getConnectionHandlers().iterator();

        while(connectIter.hasNext()) {
            ConnectionHandler connectionHandler = (ConnectionHandler)connectIter.next();
            ConnectionId connectionId = connectionHandler.getConnectionId();
            SimpleBrowserForm browserForm = previous == null ?
                    null :
                    (SimpleBrowserForm)ReflectionUtils.invokeMethod(previous, "removeBrowserForm", connectionId);
            if (browserForm == null) {
                browserForm = new SpySimpleBrowserForm(this, connectionHandler);
            } else {
                browserForm.setParentComponent(this);
            }

            JComponent component = browserForm.getComponent();
            TabInfo tabInfo = new TabInfo(component);
            tabInfo.setText((String) CommonUtil.nvl(connectionHandler.getName(), "[unnamed connection]"));
            tabInfo.setObject(browserForm);
            getConnectionTabs().addTab(tabInfo);
            EnvironmentType environmentType = connectionHandler.getEnvironmentType();
            tabInfo.setTabColor(environmentType.getColor());

            for (DBObjectBundleListener listener : ExtensionPointUtils.getDbObjectListeners(project)) {
                listener.listening("UPDATE", connectionHandler.getObjectBundle());
            }
        }

        JPanel mainPanel = (JPanel)ReflectionUtils.getFieldValue(this, "mainPanel");
        if (getConnectionTabs().getTabCount() == 0) {
            mainPanel.removeAll();
            mainPanel.add(new JBList(new ArrayList()), "Center");
        } else if (mainPanel.getComponentCount() > 0) {
            Component component = mainPanel.getComponent(0);
            if (component != getConnectionTabs()) {
                mainPanel.removeAll();
                mainPanel.add(getConnectionTabs(), "Center");
            }
        } else {
            mainPanel.add(getConnectionTabs(), "Center");
        }

    }
}
